package com.yuba.front.controller.like;

import com.yuba.common.core.constant.ActionTypes;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.validate.ActionCommentGroup;
import com.yuba.common.core.validate.ActionPosts;
import com.yuba.common.satoken.utils.LoginHelper;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.param.ActionParam;
import com.yuba.front.service.ICommentPostsService;
import com.yuba.front.service.ILikePostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帖子点赞
 *
 * @author modakai
 * @date 2024-12-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("api/like")
public class ApiLikeController extends BaseController {

    private final ILikePostsService likePostsService;
    private final ICommentPostsService commentPostsService;

    /**
     * 点赞/取消点赞帖子
     *
     * @param body 请求参数
     * @return 无
     */
    @PostMapping("posts")
    public R<Void> likePosts(@RequestBody @Validated(ActionPosts.class) ActionParam body) {
        String type = body.getType();
        if (ActionTypes.LIKE.equals(type)) {
            likePostsService.likePosts(body.getPostsId(), LoginHelper.getUserId(), body.getAuthorId());
        } else {
            likePostsService.unlikePosts(body.getPostsId(), body.getAuthorId(), LoginHelper.getUserId());
        }
        return R.ok();
    }

    /**
     * 点赞or取消点赞评论
     *
     * @param body 请求参数
     * @return 无
     */
    @PostMapping("comment")
    public R<Void> likeComments(@RequestBody @Validated(ActionCommentGroup.class) ActionParam body) {
        commentPostsService.likeComment(body.getCommentId(), body.getType(), LoginHelper.getUserId());
        return R.ok();
    }
}
